package com.example.utils;


import java.io.*;
import java.util.Properties;
/**
 * @author:我叫程晓非
 * @desc:Properties工具类
 * @date:2021-11-07
 * @version:1.0
 */
public class PropertiesUtil {
    //写
    public static boolean writeValue(String filePath, String key,String value) {
        Properties prop = new Properties();
        File file = null;
        InputStream fis = null;
        OutputStream fos = null;
        try {
            file = new File(filePath);
            if(!file.exists()){
                file.createNewFile();
            }

            fis = new FileInputStream(filePath);
            prop.load(fis);
            fos = new FileOutputStream(filePath);
            prop.setProperty(key, value);
            prop.store(fos,"Yibihuahongbao config file");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //读
    public static String readValue(String filePath, String key) {
        Properties props = new Properties();
        try (InputStream in = new BufferedInputStream(new FileInputStream(filePath))){

            props.load(in);
            String value = props.getProperty(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
